//TODO:CComboBox not inheriting CListWidget, only has a CListBox member
#include <buola/gui.h>

#include <buola/image/cimage.h>
#include <buola/gui/cicon.h>
#include <buola/image/cfont.h>
#include <buola/image/cgc.h>
#include <buola/io/cresourcefinder.h>

#include <buola/widgets/ceditbox.h>
#include <buola/widgets/ccombobox.h>
#include <buola/widgets/ctheme.h>
#include <buola/widgets/clistbox.h>

namespace buola { namespace gui {

CComboBox::CComboBox()
{
    get_default_theme()->SetBackground(this);

    SetStyle(CListBox::STYLE_LIST|CListBox::STYLE_NOICON);
    SetPointer(CPointer::STOCK_DEFAULT);

    mEdit=new CEditBox();
    mEdit->eEnter.Connect(&CComboBox::OnEditEnter,this,_1);
    mEdit->SetStyle(STYLE_NOBORDER);
    Place(mEdit);

    if(!GetStyle(CB_FREEEDIT))
        mEdit->Hide();
}

CComboBox::~CComboBox()
{
}

void CComboBox::SetCaption(const std::wstring &pCaption)
{
    mCaption=pCaption;
    mEdit->SetCaption(mCaption);
    Refresh();
}

///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
////////////////////////// Interface //////////////////////////
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
/////////////////////// Event handlers ////////////////////////
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

void CComboBox::OnExpose(const CExposeEvent &pE)
{
    get_default_theme()->DrawComboBox(pE.mGC,this,CTheme::DRAW_NONE);
}    

void CComboBox::OnListSelected(TID pID)
{
    mList->Unmap();
    mList=nullptr;
        
    SelectItemByID(pID);
}

void CComboBox::OnEditEnter(const std::wstring &pCaption)
{
    mCaption=pCaption;
    eEnter(mCaption);
}

void CComboBox::OnButtonDown(const CMouseEvent &pE)
{
    if(pE.mButton==EMouseButton::LEFT)
    {
        ///\todo complicated
    }
}

void CComboBox::OnButtonUp(const CMouseEvent &pE)
{
}

void CComboBox::OnMouseMove(const CMouseEvent &pE)
{
}

void CComboBox::OnSize(const CGeometryEvent &pE)
{
    if(GetStyle(STYLE_SMALLFONT))
        mEdit->MoveResize(CPoint_d(0,0),CSize_d(Size().x-Size().y,Size().y));
    else
        mEdit->MoveResize(CPoint_d(6,3),CSize_d(Size().x-Size().y-3,Size().y-6));
}

void CComboBox::OnStyleChanged(TStyle pOld)
{
    TStyle lChange=pOld^GetStyle();

    if(lChange&STYLE_FAKE)
    {
        if(GetStyle(STYLE_FAKE))
        {
            mEdit->SetStyleBit(STYLE_FAKE);
            SetInputMode(EInputMode::MOUSETRANSPARENT);
        }
        else
        {
            mEdit->UnsetStyleBit(STYLE_FAKE);
            SetInputMode(EInputMode::NORMAL);
        }
    }
    if(lChange&CB_FREEEDIT)
    {
        if(GetStyle(CB_FREEEDIT))
            mEdit->Show();
        else
            mEdit->Hide();
    }
    if(lChange&STYLE_SMALLFONT)
    {
        if(GetStyle(STYLE_SMALLFONT))
            mEdit->SetStyleBit(STYLE_SMALLFONT);
        else
            mEdit->UnsetStyleBit(STYLE_SMALLFONT);
        OnSize(CGeometryEvent());
    }

    Refresh();
}

void CComboBox::OnItemSelected(SItem *pI)
{
    if(pI->mSelected)
    {
        sSelected(pI,pI->mID);

        SetCaption(pI->mText);
    }
}

/*namespace gui*/ } /*namespace buola*/ }
